package com.sakura.mybatis.utils;

import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 命名风格转换工具类
 */
public class NamingConverter {

    /**
     * 将Java驼峰命名转换为数据库下划线命名
     * @param camelCaseName Java驼峰命名
     * @return 数据库下划线命名
     */
    public static String camelToUnderline(String camelCaseName) {
        if (camelCaseName == null || camelCaseName.isEmpty()) {
            return camelCaseName;
        }

        StringBuilder result = new StringBuilder();
        result.append(Character.toLowerCase(camelCaseName.charAt(0)));
        
        for (int i = 1; i < camelCaseName.length(); i++) {
            char currentChar = camelCaseName.charAt(i);
            if (Character.isUpperCase(currentChar)) {
                result.append('_').append(Character.toLowerCase(currentChar));
            } else {
                result.append(currentChar);
            }
        }
        
        return result.toString();
    }

    /**
     * 将数据库下划线命名转换为Java驼峰命名
     * @param underlineName 数据库下划线命名
     * @return Java驼峰命名
     */
    public static String underlineToCamel(String underlineName) {
        if (underlineName == null || underlineName.isEmpty()) {
            return underlineName;
        }

        StringBuilder result = new StringBuilder();
        boolean nextUpper = false;
        
        for (int i = 0; i < underlineName.length(); i++) {
            char currentChar = underlineName.charAt(i);
            if (currentChar == '_') {
                nextUpper = true;
            } else {
                if (nextUpper) {
                    result.append(Character.toUpperCase(currentChar));
                    nextUpper = false;
                } else {
                    result.append(Character.toLowerCase(currentChar));
                }
            }
        }
        
        return result.toString();
    }

    /**
     * 批量转换类字段名到数据库命名
     * @param clazz Java类
     * @return 字段名映射Map (Java字段名 -> 数据库字段名)
     */
    public static Map<String, String> convertClassFieldsToUnderline(Class<?> clazz) {
        Map<String, String> fieldMap = new LinkedHashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        
        for (Field field : fields) {
            String javaName = field.getName();
            String dbName = camelToUnderline(javaName);
            fieldMap.put(javaName, dbName);
        }
        
        return fieldMap;
    }

    // 测试
    public static void main(String[] args) {
        // 单个字段转换测试
        System.out.println(camelToUnderline("userName"));      // 输出: user_name
        System.out.println(camelToUnderline("UserDTO"));      // 输出: user_dto
        System.out.println(camelToUnderline("orderID"));       // 输出: order_id
        
        System.out.println(underlineToCamel("user_name"));     // 输出: userName
        System.out.println(underlineToCamel("order_detail"));  // 输出: orderDetail
        
        // 类字段批量转换测试
        class TestClass {
            private String userName;
            private int age;
            private String homeAddress;
            // 省略getter/setter
        }
        
        Map<String, String> fieldMap = convertClassFieldsToUnderline(TestClass.class);
        fieldMap.forEach((k, v) -> System.out.println(k + " -> "));
    }
}